import axios from '@/libs/axios1'
let jsonHeaders = {'content-type': 'application/json;charset=UTF-8'}
let url = '/exampaper'
let exam = '/self-study/exam'
//创建试卷
export const createTestPaper = params =>{
  return axios({
    // url:url + `/testPaper/createTestPaper`,
    url:`/new-paper/paper/create`,
    method:'POST',
    headers:jsonHeaders,
    data:params
  })
}

//修改试卷
export const modifyTestPaper = (params,id) =>{
  return axios({
    // url:url + `/testPaper/updateTestPaper`,
    url:`/new-paper/paper/${id}`,
    method:'PUT',
    headers:jsonHeaders,
    data: params,
  })
}

//删除试卷
export const delTestPaper = id =>{
  return axios({
    // url:url + `/testPaper/delTestPaper`,
    url:`/new-paper/paper/${id.paperId}`,
    method:'DELETE',
    headers:jsonHeaders
  })
}


//查询试卷列表
export const queryTestPaperList = (params,query) =>{
  return axios({
    // url:url + `/testPaper/queryTestPaper`,
    url:`/new-paper/paper/list`,
    method: 'POST',
    data:params,
    params:query,
    headers:jsonHeaders
  })
}
//查询试卷列表中题目信息
export const queryTestPaperQuestionList = params =>{
  return new Promise(resolve => {
		axios({
      // url:url + `/testPaper/queryTestPaperInfo`,
      url:`/new-paper/paper/detail/${params.paperId}`,
      method:'GET',
      headers:jsonHeaders
    }).then(async res => {
			if (res && res.data && res.data.paperQuestion) {
				// 将接口返回的数据结构转化为页面所需的数据结构
				res.data.paperQuestion.forEach(item => {
					item.answers.forEach((subAnswer, index) => {
            if(subAnswer.description.indexOf('】') == 2 || subAnswer.description.indexOf(']') == 2) {
            subAnswer.description = subAnswer.description.slice(3);
            }
            if (['1', '2'].includes(item.questionType)) {
            subAnswer.description = `[${noFilter(index)}]${subAnswer.description}`
            }
          })
				})
			}
			resolve(res)
		})
	})
}

const noFilter = (val) => {
  let num = val + 1
  let str;
  switch(num) {
    case 1:
      str = 'A';
      break;
    case 2:
      str = 'B';
      break;
    case 3:
      str = 'C';
      break;
    case 4:
      str = 'D';
      break;
    case 5:
      str = 'E';
      break;
    case 6:
      str = 'F';
      break;
    case 7:
      str = 'G';
      break;
    case 8:
      str = 'H';
      break;
    case 9:
      str = 'I';
      break;
    case 10:
      str = 'J';
      break;
    case 11:
      str = 'K';
      break;
    case 12:
      str = 'L';
      break;
    default:
      str = '';
      break;
  }
  return str;
}

//查询题目详情
export const queryQuestionInfo = params =>{
  return axios({
    url:url + `/testPaper/queryQuestionInfo`,
    method:'GET',
    params:params,
    headers:jsonHeaders
  })
}

export const paperGetUsedTimes = (data) => {
  return axios({
    url:`${exam}/userAnswer/getUsedTimes`,
    method: 'post',
    data,
    headers: jsonHeaders
  })
}